package com.qianniu.napi.admin.web;

import com.alibaba.fastjson.JSON;
import com.qianniu.napi.admin.entity.MconfigExt;
import com.qianniu.napi.admin.entity.MerchantApp;
import com.qianniu.napi.admin.entity.Role;
import com.qianniu.napi.admin.entity.User;
import com.qianniu.napi.admin.service.IRedisService;
import com.qianniu.napi.common.util.ObjectUtil;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chenxuebin on 2017/2/28.
 */
@Controller
public class IndexController {

    private static Logger logger = LoggerFactory.getLogger(IndexController.class);
    @Autowired
    private IRedisService redisService;

    @RequestMapping("/home")
    public String indexHome(Model model, HttpServletRequest request) {
        logger.info("=========welcome to /home");
        User bean = (User) SecurityUtils.getSubject().getPrincipal();
        MerchantApp mapp = redisService.getMApp(ObjectUtil.getMpid());

        MconfigExt mconfigExt = redisService.getMconfigExt(ObjectUtil.getMpid());
        int hytype = mapp != null && mapp.getVersion() == 4 ? 1 : 0;

        model.addAttribute("hytype", hytype);
        model.addAttribute("photo", bean.getPhoto());
        logger.info("=========login photo=" + bean.getPhoto());

        model.addAttribute("utype", bean.getUserType());
        logger.info("=========login utype=" + bean.getUserType());

        model.addAttribute("umpid", bean.getMpid());
        logger.info("=========login umpid=" + (bean.getMpid() == null ? 0 : bean.getMpid()));

        model.addAttribute("uname", bean.getUsername());
        logger.info("=========login uname=" + bean.getUsername());

        model.addAttribute("cname", bean.getName());
        logger.info("=========login cname=" + bean.getName());

        List<Role> roles = bean.getRoleList();
        Long roleid = 1l;

        for (Role item:roles){
            if(item.getId().intValue()==11) roleid=11l;
        }
        model.addAttribute("roleid", roleid);
        logger.info("===========================login roles=" + JSON.toJSONString(roles));

        model.addAttribute("templatemsg", mconfigExt != null && mconfigExt.getTemplatemsg() == 1 ? 1 : -1);
        model.addAttribute("tplform", mconfigExt != null && mconfigExt.getTplform() == 1 ? 1 : -1);
        model.addAttribute("version", mapp != null ? mapp.getVersion() : 0);

        setLogininfo(model, request);

        return "common/frame";
    }

//    @RequestMapping("/")
//    public String index(Model model,HttpServletRequest request) {
//        logger.info("=========welcome to index /");
//        User bean = (User) SecurityUtils.getSubject().getPrincipal();
//        model.addAttribute("photo", bean.getPhoto());
//        logger.info("=========login photo=" + bean.getPhoto());
//
//        model.addAttribute("utype", bean.getUserType());
//        logger.info("=========login utype=" + bean.getUserType());
//
//        model.addAttribute("uname", bean.getUsername());
//        logger.info("=========login uname=" + bean.getUsername());
//
//        model.addAttribute("cname", bean.getName());
//        logger.info("=========login cname=" + bean.getName());
//        setLogininfo(model,request);
//
//        MconfigExt mconfigExt = redisService.getMconfigExt(ObjectUtil.getMpid());
//
//        model.addAttribute("templatemsg", mconfigExt!=null&&mconfigExt.getTemplatemsg()==1?1:-1);
//        model.addAttribute("tplform", mconfigExt!=null&&mconfigExt.getTplform()==1?1:-1);
//        model.addAttribute("version", mapp.getVersion());
//
//        return "common/frame";
//    }

    @RequestMapping("/index")
    public String list(Model model) {
        System.out.println("this is index");
        return "index";
    }

    @RequestMapping("/team")
    public String team(Model model) {
        System.out.println("this is team");
        Map<String, String> obj = new HashMap<String, String>();
        obj.put("name", "yii");
        model.addAttribute("son", obj);
        return "team";
    }

    private void setLogininfo(Model model, HttpServletRequest request) {

        logger.info(">>>request.getServerName()=" + request.getServerName());
        if (request.getServerName().toLowerCase().startsWith("fmall")) {
            logger.info(">>>setLogininfo to fmall");
            model.addAttribute("jtitle", "LCFL");
            model.addAttribute("title", "欢迎使用理财返利后台管理系统");
        } else if (request.getServerName().toLowerCase().startsWith("huakou")) {
            logger.info(">>>setLogininfo to taozi");
            model.addAttribute("jtitle", "TZ");
            model.addAttribute("bgurl", "TZ");
            model.addAttribute("title", "欢迎使用华口云后台管理系统");
        } else if (request.getServerName().toLowerCase().startsWith("vms")) {
            logger.info(">>>setLogininfo to taozi");
            model.addAttribute("jtitle", "TZ");
            model.addAttribute("title", "欢迎使用桃子股票分析系统");
        } else if (request.getServerName().toLowerCase().startsWith("open")) {
            logger.info(">>>setLogininfo to qianniu");
            model.addAttribute("jtitle", "OPEN");
            model.addAttribute("title", "欢迎使用仟牛后台管理系统");
        }
    }

}
